package com.yjzx.care.base;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author yjzx
 * @date 2023/12/28
 * @description 单对象引用（防止对象变更造成丢失）
 */
public class Value<T> implements ValueGetSet<T>, Serializable {
    private volatile T value;

    private static final long serialVersionUID = 1L;

    private Value(T value) {
        this.value = value;
    }

    @Override
    public T get() {
        return value;
    }

    @Override
    public void set(T t) {
        this.value = t;
    }

    public static <T> Value<T> of(T value) {
        return new Value<>(value);
    }

    public  boolean isEmpty() {
        return value == null;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Value<?> value = (Value<?>) o;
        return Objects.equals(this.value, value.value);
    }

    @Override
    public int hashCode() {
        return value != null ? value.hashCode() : 0;
    }

}